package com.zhouzi.zhaoyang.mvp

import com.zhouzi.zhaoyang.http.BaseBean
import com.zhouzi.zhaoyang.http.RetrofitFactory
import com.zhouzi.zhaoyang.http.SThrowable
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine

/**
 * Created by ZhouZi on 2019/7/4.
 * time:16:03
 * ----------Dragon be here!----------/
 * 　　　┏┓　　 ┏┓
 * 　　┏┛┻━━━┛┻┓━━━
 * 　　┃　　　　　 ┃
 * 　　┃　　　━　  ┃
 * 　　┃　┳┛　┗┳
 * 　　┃　　　　　 ┃
 * 　　┃　　　┻　  ┃
 * 　　┃　　　　   ┃
 * 　　┗━┓　　　┏━┛Code is far away from bug with the animal protecting
 * 　　　　┃　　　┃    神兽保佑,代码无bug
 * 　　　　┃　　　┃
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛━━━━━
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ━━━━━━━━━━━神兽出没━━━━━━━━━━━━━━
 */
class ModelLoader {



    suspend fun getAuthCode(map: MutableMap<String, String>) =
        RetrofitFactory.getInstence().api().getAuthCode(map).await()

    suspend fun payOrder(map: MutableMap<String, String>) =
        RetrofitFactory.getInstence().api().payOrder(map).await()

    suspend fun login(map: MutableMap<String, String>) =
        RetrofitFactory.getInstence().api().login(map).await()

    suspend fun getOrderNo(deviceNo: String) =
        RetrofitFactory.getInstence().api().getOrderNo(deviceNo).await()

//    suspend fun getOrder(outTradeNo: String) =
//        RetrofitFactory.getInstence().api().getOrder(outTradeNo).await()

    suspend fun getMemberPrice(deviceNo: String, totalFee: String, phone: String) =
        RetrofitFactory.getInstence().api().getMemberPrice(deviceNo, totalFee, phone).await()

    suspend fun logout() =
        RetrofitFactory.getInstence().api().logout().await()

    suspend fun getAdvertList(deviceNo: String) =
        RetrofitFactory.getInstence().api().getAdvertList(deviceNo).await()


    private suspend fun <T> Call<BaseBean<T>>.await(): T? {
        return suspendCoroutine { continuation ->
            enqueue(object : Callback<BaseBean<T>> {
                // bean 对象的封装
                override fun onFailure(call: Call<BaseBean<T>>, t: Throwable) {
                    t.printStackTrace()
                    continuation.resumeWithException(SThrowable("-1", "服务器连接超时，请稍后再试"))
                }

                override fun onResponse(call: Call<BaseBean<T>>, response: Response<BaseBean<T>>) {
                    val body = response.body()
                    if (body != null) {
                        when {
                            body.code == "200" -> continuation.resume(body.result)
                            else -> continuation.resumeWithException(
                                SThrowable(
                                    body.code,
                                    body.message
                                )
                            )
                        }
                    } else continuation.resumeWithException(SThrowable("-1", "服务器连接超时，请稍后再试"))
                }

            })
        }
    }


    suspend fun getOrder(outTradeNo: String?)  =
        RetrofitFactory.getInstence().api().getOrder(outTradeNo).await()


    suspend fun getAppQrCode(deviceNo: String) =
        RetrofitFactory.getInstence().api().getAppQrCode(deviceNo).await()


    suspend fun newest(versionNo: String) =
        RetrofitFactory.getInstence().api().newest(versionNo).await()


    suspend fun memberPay(map: MutableMap<String, String>) =
        RetrofitFactory.getInstence().api().memberPay(map).await()


    suspend fun payDepositorder(map: MutableMap<String, String>) =
        RetrofitFactory.getInstence().api().payDepositorder(map).await()


    suspend fun returnDeposit(map: MutableMap<String, String>) =
        RetrofitFactory.getInstence().api().returnDeposit(map).await()


    suspend fun createScanOrder(map: MutableMap<String, Any?>) =
        RetrofitFactory.getInstence().api().createScanOrder(map).await()


    suspend fun getCloseAccount(deviceNo: String) =
        RetrofitFactory.getInstence().api().getCloseAccount(deviceNo).await()

    suspend fun getLastOrderPrint(deviceNo: String) =
        RetrofitFactory.getInstence().api().getLastOrderPrint(deviceNo).await()

    suspend fun updatePassword(map: MutableMap<String, String>) =
        RetrofitFactory.getInstence().api().updatePassword(map).await()

    suspend fun refund(map: MutableMap<String, String>) =
        RetrofitFactory.getInstence().api().refund(map).await()

    suspend fun getOrderList(pageNum: Int, pageSize: Int, map: MutableMap<String, Any>) =
        RetrofitFactory.getInstence().api().getOrderList(pageNum, pageSize, map).await()

    suspend fun getCloseAccountPrint(map: MutableMap<String, Any>) =
        RetrofitFactory.getInstence().api().getCloseAccountPrint(map).await()

    suspend fun getOrderListPrint(map: MutableMap<String, String>) =
        RetrofitFactory.getInstence().api().getOrderListPrint(map).await()


    suspend fun selectCloseAccountList(pageNum: Int, pageSize: Int,map: MutableMap<String, String>) =
        RetrofitFactory.getInstence().api().selectCloseAccountList(pageNum, pageSize,map).await()

}